package com.example.myapplication

import android.content.Context
import android.location.Criteria
import android.location.Location
import android.location.LocationManager
import android.location.provider.ProviderProperties
import android.os.Handler
import android.os.Looper
import android.os.SystemClock

class MockLocationHelper(private val context: Context) {

    private val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
    private var handler: Handler? = null
    private var mockRunnable: Runnable? = null

    fun setMockLocation(latitude: Double, longitude: Double) {
        val mockLocation = Location(LocationManager.GPS_PROVIDER).apply {
            this.latitude = latitude
            this.longitude = longitude
            this.altitude = 30.0
            this.bearing=180.0f
            this.speed=10f
            this.time = System.currentTimeMillis()
            this.accuracy = 0.1f
            this.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
        }


        try {
//            val provider = locationManager.getProvider(LocationManager.GPS_PROVIDER)
//            if (provider == null){
//                locationManager.addTestProvider(
//                    LocationManager.GPS_PROVIDER,
//                    true, true, false, false, true, true, true,
//                    ProviderProperties.POWER_USAGE_LOW, ProviderProperties.ACCURACY_FINE
//                )
//            }

            locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mockLocation)
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: SecurityException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun enableMockLocation() {
        try {
            if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
                locationManager.addTestProvider(
                    LocationManager.GPS_PROVIDER,
                    true, true, false, false, true, true, true, ProviderProperties.POWER_USAGE_LOW, ProviderProperties.ACCURACY_FINE
                )
            }

            locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true)
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: SecurityException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun startMocking(latitude: Double, longitude: Double) {
        enableMockLocation()

        handler = Handler(Looper.getMainLooper())
        mockRunnable = object : Runnable {
            override fun run() {
                setMockLocation(latitude, longitude)
                handler?.postDelayed(this, 50)  // 每2秒更新一次位置
            }
        }
        handler?.post(mockRunnable!!)
    }

    fun stopMocking() {
        handler?.removeCallbacks(mockRunnable!!)
        disableMockLocation()
    }

    fun disableMockLocation() {
        try {
            if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
                locationManager.removeTestProvider(LocationManager.GPS_PROVIDER)
            }
        } catch (e: IllegalArgumentException) {
            e.printStackTrace()
        } catch (e: SecurityException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
